package com.cloudcc.mobile.network;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;

import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.entity.chat.DraftBox;
import com.cloudcc.mobile.entity.comment.AddMicroComment;
import com.cloudcc.mobile.entity.comment.AddMicroCommentMessage;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.parser.AddMicroCommentParser;
import com.cloudcc.mobile.parser.ReturnCodeParser;
import com.cloudcc.mobile.util.Json;
import com.cloudcc.mobile.util.NetWork;
import com.cloudcc.mobile.util.NotificationUtils;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.util.UrlTools;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;

/**
 * 后台发送评论
 * @author Administrator
 *
 */
public class AddMicroCommentUtil{
	private Context context;
	private int num;

	AddMicroComment addMicroComment;
	String data;

	public AddMicroCommentUtil(Context context,int num){
		this.context=context;
		this.num=num;
	}

	
	/**
	 * 后台发送"回复"
	 */
	public void sendComment(AddMicroComment addMicroComment){
		NotificationUtils.showNotification(context, "发送中...", "回复", "微贴发送中...", num);
		this.addMicroComment=addMicroComment;
		String data=Json.toJson(addMicroComment);
		new MyTask().execute(data);
	}
	/**	 * 添加评论	 */
	class MyTask extends AsyncTask<String, Void, String>{
		@Override
		protected String doInBackground(String... params) {
			String data=params[0];
			String json=null;
			//			String json=serivice.addMicroComment(data);
			List<NameValuePair> pairs=new ArrayList<NameValuePair>();
			pairs.add(new BasicNameValuePair("serviceName", AppContext.ADDMICCOMMENT));
			pairs.add(new BasicNameValuePair("data", data));
			pairs.add(new BasicNameValuePair("binding", AppContext.binding));
			String url=UrlTools.url+UrlTools.url_path;
			json=NetWork.post(url, pairs);
			return json;
		}
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			try {
				if(result==null||result.equals("NetworkError")){
					SaveCaoGao();
					NotificationUtils.clearNotification(context, num);
					Tools.showInfo(context, "网络异常,回复已保存到草稿箱");
					return ;
				}
				if(result.equals("outTime")){
					SaveCaoGao();
					NotificationUtils.clearNotification(context, num);
					Tools.showInfo(context, "网络超时,回复已经保存到草稿箱");
					return;
				}
				String code=ReturnCodeParser.getReturnCode(result);
				if(code.equals("-2")){
					try {
						SaveCaoGao();
					} catch (Exception e) {
						Tools.handle(e);
					}
					NotificationUtils.clearNotification(context, num);
					Tools.showInfo(context, "登陆超时,请重新登陆");
					return;
				}
				AddMicroCommentMessage msg=AddMicroCommentParser.addmicrocommnet(result);
				try {
					String id=msg.getId();
					if(id.equals("")||id==null||id.equals("null")){
						Tools.showInfo(context, "亲~，此贴已被删除,请刷新数据");
						NotificationUtils.clearNotification(context, num);
						Intent intent=new Intent(AppContext.INDEXADAPTER_SHUAXIN);
						context.sendBroadcast(intent);
						return;
					}
				} catch (Exception e) {
					Tools.handle(e);
				}
				boolean flag=msg.isResult();
				if(flag){
					Tools.showInfo(context, "回复成功");
					NotificationUtils.clearNotification(context, num);
					Intent intent=new Intent(AppContext.ADDMICRO_COMMENT_SHUAXIN);
					context.sendBroadcast(intent);
				}else{
					SaveCaoGao();
					NotificationUtils.clearNotification(context, num);
					Tools.showInfo(context, "回复失败,已经保存到草稿箱");
				}
			} catch (Exception e) {
				Tools.handle(e);
			}
		}
	}
	
	/**	 * 把回复保存到草稿箱	 */
	private void SaveCaoGao() {
		try {
			saveDraftComment();
		} catch (DbException e) {
			Tools.handle(e);
		}
		Intent caogao=new Intent(AppContext.CAOGAO_NUM);
		context.sendBroadcast(caogao);
	}
	/**	 * 把回复保存到数据库	 */
	private void saveDraftComment() throws DbException {
		DbUtils db=DbUtils.create(context, UserManager.getManager().getUser().userId+"box.db");
		DraftBox box=new DraftBox();
		box.setType("3");
		box.setTime(Tools.getTime(System.currentTimeMillis()));
		box.setIsNew("1");
		box.setLeixing(AppContext.ADDMICCOMMENT);
		addMicroComment.setParent(box);
		box.setComment(addMicroComment);
		box.setClickable(true);
		db.saveBindingId(addMicroComment);
	}
}
